//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.visualdata.controller;

import ciicos.base.ActionResult;
import ciicos.base.controller.SuperController;
import ciicos.util.JsonUtil;
import ciicos.visualdata.entity.VisualCategoryEntity;
import ciicos.visualdata.model.VisualPageVO;
import ciicos.visualdata.model.VisualPagination;
import ciicos.visualdata.model.visualcategory.VisualCategoryCrForm;
import ciicos.visualdata.model.visualcategory.VisualCategoryInfoVO;
import ciicos.visualdata.model.visualcategory.VisualCategoryListVO;
import ciicos.visualdata.model.visualcategory.VisualCategoryUpForm;
import ciicos.visualdata.service.VisualCategoryService;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Tag(
        name = "大屏分类",
        description = "category"
)
@RequestMapping({"/api/blade-visual/category"})
public class VisualCategoryController extends SuperController<VisualCategoryService, VisualCategoryEntity> {
    @Autowired
    private VisualCategoryService categoryService;

    @Operation(
            summary = "列表"
    )
    @GetMapping({"/page"})
    @SaCheckPermission({"onlineDev.dataScreen"})
    public ActionResult<VisualPageVO<VisualCategoryListVO>> list(VisualPagination pagination) {
        List<VisualCategoryEntity> data = this.categoryService.getList(pagination);
        List<VisualCategoryListVO> list = JsonUtil.getJsonToList(data, VisualCategoryListVO.class);
        VisualPageVO paginationVO = (VisualPageVO)JsonUtil.getJsonToBean(pagination, VisualPageVO.class);
        paginationVO.setRecords(list);
        return ActionResult.success(paginationVO);
    }

    @Operation(
            summary = "列表"
    )
    @GetMapping({"/list"})
    @SaCheckPermission({"onlineDev.dataScreen"})
    public ActionResult<List<VisualCategoryListVO>> list() {
        List<VisualCategoryEntity> data = this.categoryService.getList();
        List<VisualCategoryListVO> list = JsonUtil.getJsonToList(data, VisualCategoryListVO.class);
        return ActionResult.success(list);
    }

    @Operation(
            summary = "详情"
    )
    @GetMapping({"/detail"})
    @Parameters({@Parameter(
            name = "id",
            description = "主键",
            required = true
    )})
    public ActionResult<VisualCategoryInfoVO> info(@RequestParam("id") String id) {
        VisualCategoryEntity entity = this.categoryService.getInfo(id);
        VisualCategoryInfoVO vo = (VisualCategoryInfoVO)JsonUtil.getJsonToBean(entity, VisualCategoryInfoVO.class);
        return ActionResult.success(vo);
    }

    @Operation(
            summary = "新增"
    )
    @PostMapping({"/save"})
    @Parameters({@Parameter(
            name = "categoryCrForm",
            description = "大屏分类模型",
            required = true
    )})
    @SaCheckPermission({"onlineDev.dataScreen"})
    public ActionResult create(@RequestBody @Valid VisualCategoryCrForm categoryCrForm) {
        VisualCategoryEntity entity = (VisualCategoryEntity)JsonUtil.getJsonToBean(categoryCrForm, VisualCategoryEntity.class);
        if (this.categoryService.isExistByValue(entity.getCategoryvalue(), entity.getId())) {
            return ActionResult.fail("模块键值已存在");
        } else {
            this.categoryService.create(entity);
            return ActionResult.success("新建成功");
        }
    }

    @Operation(
            summary = "修改"
    )
    @PostMapping({"/update"})
    @Parameters({@Parameter(
            name = "categoryUpForm",
            description = "大屏分类模型",
            required = true
    )})
    @SaCheckPermission({"onlineDev.dataScreen"})
    public ActionResult update(@RequestBody VisualCategoryUpForm categoryUpForm) {
        VisualCategoryEntity entity = (VisualCategoryEntity)JsonUtil.getJsonToBean(categoryUpForm, VisualCategoryEntity.class);
        if (this.categoryService.isExistByValue(entity.getCategoryvalue(), entity.getId())) {
            return ActionResult.fail("模块键值已存在");
        } else {
            boolean flag = this.categoryService.update(categoryUpForm.getId(), entity);
            return !flag ? ActionResult.fail("更新失败，数据不存在") : ActionResult.success("更新成功");
        }
    }

    @Operation(
            summary = "删除"
    )
    @PostMapping({"/remove"})
    @Parameters({@Parameter(
            name = "ids",
            description = "主键",
            required = true
    )})
    @SaCheckPermission({"onlineDev.dataScreen"})
    public ActionResult delete(String ids) {
        VisualCategoryEntity entity = this.categoryService.getInfo(ids);
        if (entity != null) {
            this.categoryService.delete(entity);
            return ActionResult.success("删除成功");
        } else {
            return ActionResult.fail("删除失败，数据不存在");
        }
    }
}
